package ag.carnot.pinkblob.web.jsf.beans.pinkblob.issueinvoice;

import ag.carnot.web.jsf.common.structureddata.ComplexTypeWrapper;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;

import org.apache.log4j.Logger;

import pinkblob.log.Log;

public class CreateBillCommentBean {

	private static Logger log = new Log().getLogger(CreateBillCommentBean.class);

	public void complete(){
		
		  log.info("Completing "+this);
		
		  // variables that have to be filled for completion		  
		  String pid = (String)getProjectInformation().get("ProjectID");
		  Float price = (Float) getProjectInformation().get("Price");
		  String comment = (String) getProjectInformation().get("II_BillMailComment");  
		  
		  log.info("Pid "+ pid + ", Price " + price + ", Comment "+ comment);
		  
		  // create message to display
		  FacesMessage msg = new FacesMessage();
		  String summary = "";
		  if(pid == null || pid.trim().equals("")){
			  summary = "Project ID not set or unknown!";		  
		  }
		  if(price == null || price.floatValue() == 0.0f){
			  summary = summary + " You are required to set a price!";		  		 
		  }
		  if(comment == null || comment.trim().equals("")){
			  summary = summary + " You are required to set a comment!";		  
		  }	  		  	  
		  if(!(summary.equals(""))){
			  log.error(summary);
			  msg.setSummary(summary);
			  throw new ValidatorException(msg);
		  }	
	}
		  
	private ComplexTypeWrapper projectInformationWrapper = new ComplexTypeWrapper(
			"ProjectInformation", null);

	public ComplexTypeWrapper getProjectInformationWrapper() {
		return projectInformationWrapper;
	}

	@SuppressWarnings("unchecked")
	public Map getProjectInformation() {
		return (Map) projectInformationWrapper.getComplexType();
	}

	@SuppressWarnings("unchecked")
	public void setProjectInformation(Map projectInformation) {
		projectInformationWrapper = new ComplexTypeWrapper(
				projectInformationWrapper.getDataMappingId(),
				projectInformation);
	}
}
